package ArbejdemedFilerogBiblioteker;

import java.io.File;

public class FileInfo {
	private File dir;
	private String type;

	public FileInfo(String dir) {
		this.dir = new File(dir);
		System.out.println("The directory " + dir
							+ " contains " + countFiles()
							+ " files with a total size of" + measureSize()
							+ " bytes.");
	}
 
	public FileInfo(String dir, String type) {
		this.dir = new File(dir);
		this.type = type;
		System.out.println("The directory " + dir
							+ " contains " + countFiles()
							+ " files of the type " + type
							+ " with a total size of" + measureSize()
							+ " bytes.");
	}

	public int countFiles() {
		if (type != null) {
			int files = 0;
			for (File file : dir.listFiles()) {
				if (file.getName().endsWith(type)) {
					files++;
				}
			}
			return files;
		}
		return dir.listFiles().length;	
	}

	public int measureSize() {
		int fileSize = 0;
		if (type != null) {
			for (File file : dir.listFiles()) {
				if (file.getName().endsWith(type)) {
					fileSize += file.length();
				}
			}
		} else {
			for (File file : dir.listFiles()) {
				fileSize += file.length();
			}
		}
		return fileSize;
	}

	//Remember to put the path argument in double quotes
	//Ie. "c:\oop\opgaver"
	public static void main(String[] args) {
		if (args.length > 0) {
			if (args.length > 2 && args[1].equals("-type")) {
				new FileInfo(args[0], args[2]);
			} else {
				new FileInfo(args[0]);
			}
		}
	}
}